﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_DAL;
using System.Data.Common;
using Nova_Common;
using System.Text;
using System.Collections.Generic;
using Nova_BLL;
using Nova_MOD;
using Nova_Common.Extend;


//userlist.aspx 中的添加、编辑转到次页面（带参数，mode,userno,pdi）
public partial class System_User_UserListAdd : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    DbHelper dbHelper = new DbHelper();
    DbCommand cmd = null;

    Nova_MOD.Users Nova_MOD_Users = new Nova_MOD.Users();
    Nova_BLL.Users Nova_BLL_Users = new Nova_BLL.Users();
    int iCount = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = 0;  //0表示立即过期
        Response.Cache.SetNoStore();//禁用缓存
        Response.AppendHeader("Pragma", "no-cache");// 是不要网页存于缓存之中
        LoginLogic.IsLogin();
        if (!IsPostBack)
        {
            Bind_WareHs();
            SetFirstPageLoad();
        }

        //验证TextBox
        string strJs = "";
        strJs = "return Validate(";
        strJs += "'txtvUserNo','用户编号不为空','r'";
        strJs += ",'txtvUserName','用户名称不为空','r'";
        strJs += ");";
        ibtnOK.Attributes.Add("onclick", strJs);
        ibtnContinue.Attributes.Add("onclick", strJs);
    }

    private void Bind_WareHs()
    {
        string strSQL = "SELECT * FROM TB_WareHs";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        ds = dbHelper.ExecuteDataSet(cmd);
        ddl_WareHs.DataSource = ds;
        ddl_WareHs.DataBind();
    }

    private void SetFirstPageLoad()
    {
        BindDepart();
        BindGroup();

        if (Request.QueryString["Mode"] == "Add")
        {
            txtvPassword.Text = "123456";
            panelContinue.Visible = true;
            lblTitle.Text = "系统管理-用户新增";
        }
        else if (Request.QueryString["Mode"] == "Update")
        {
            lblTitle.Text = "系统管理-用户修改";
            panelContinue.Visible = false;
            ds = Nova_BLL_Users.GetUserDataSet(Request.QueryString["id"]);//前一个页面传过来的参数PID
            if (ds.Tables[0].Rows.Count > 0)
            {
                txtvUserNo.Text = ds.Tables[0].Rows[0]["vUserNo"].ToString().Trim();
                txtvUserName.Text = ds.Tables[0].Rows[0]["vUserName"].ToString().Trim();
                txtvPassword.Text = ds.Tables[0].Rows[0]["vPassWord"].ToString().Trim();
                ddlvDepartment.SelectedValue = ds.Tables[0].Rows[0]["vDepartmentNo"].ToString().Trim();
                ddlvGroup.SelectedValue = ds.Tables[0].Rows[0]["vGroupNo"].ToString().Trim();
                txtvIpAddress.Text = ds.Tables[0].Rows[0]["vIpAddress"].ToString().Trim();
                rblvSex.SelectedValue = ds.Tables[0].Rows[0]["vSex"].ToString().Trim();
                rbliUserStatus.SelectedValue = ds.Tables[0].Rows[0]["iUserStatus"].ToString().Trim();
                ddl_WareHs.SelectedValue = ds.Tables[0].Rows[0]["WID"].ToString().Trim();
            }
        }
    }


    private void BindDepart()
    {
        Nova_BLL.DepartMent Nova_BLL_DepartMent = new Nova_BLL.DepartMent();
        ds = Nova_BLL_DepartMent.DataListvDepartment();
        ddlvDepartment.DataTextField = "vDepartment";
        ddlvDepartment.DataValueField = "vDepartmentNo";
        ddlvDepartment.DataSource = ds;
        ddlvDepartment.DataBind();
    }

    private void BindGroup()
    {
        Nova_BLL.Group Nova_BLL_Group = new Nova_BLL.Group();
        ds = Nova_BLL_Group.DataListGroup();
        ddlvGroup.DataTextField = "vGroupName";
        ddlvGroup.DataValueField = "vGroupNo";
        ddlvGroup.DataSource = ds;
        ddlvGroup.DataBind();
    }

    protected void ibtnOK_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            GetUsersMode();
            if (Request.QueryString["Mode"] == "Add")
            {
                iCount = Nova_BLL_Users.AddUser(Nova_MOD_Users);
            }
            else if (Request.QueryString["Mode"] == "Update")
            {
                Nova_MOD_Users.PID = Request.QueryString["id"].ToString();
                iCount = Nova_BLL_Users.UpdateUser(Nova_MOD_Users, Request.QueryString["UserNo"]);

            }
             
            if (iCount == 1)
            {
                ShowBox.MessageBox(this.Page, "新增|修改用户成功!");
                //ibtnOK.Attributes.Add("onclick", "javascript:window.returnValue=123;window.close()");
                this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova1", "window.opener.document.getElementById('hidden').value='1';window.opener.document.getElementById('form1').submit();top.close();", true);
                return;
            }
            else if (iCount == 2)
            {
                ShowBox.MessageBox(this.Page, "用户编号已经存在!");
                return;

            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }

    }
    protected void ibtnContinue_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            GetUsersMode();
            iCount = Nova_BLL_Users.AddUser(Nova_MOD_Users);
            if (iCount == 1)
            {
                ClearControl();
                return;
            }
            else if (iCount == 2)
            {
                ShowBox.MessageBox(this.Page, "用户编号已经存在!");
                return;

            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    protected void ibtnClose_Click(object sender, ImageClickEventArgs e)
    {
        //ibtnClose.Attributes.Add("onclick", "javascript:top.close()");
        this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova", "top.close();", true);

    }

    private void GetUsersMode()
    {
        Nova_MOD_Users.vUserNo = txtvUserNo.Text.ToString().Trim();
        Nova_MOD_Users.vUserName = txtvUserName.Text.ToString().Trim();
        Nova_MOD_Users.vPassWord = txtvPassword.Text.ToString().Trim();
        if (ddlvDepartment.Items.Count > 0)
        {
            Nova_MOD_Users.vDepartmentNo = ddlvDepartment.SelectedValue.ToString();
        }
        if (ddlvGroup.Items.Count > 0)
        {
            Nova_MOD_Users.vGroupNo = ddlvGroup.SelectedValue.ToString();
        }
        Nova_MOD_Users.vIpAddress = txtvIpAddress.Text.ToString().Trim();
        Nova_MOD_Users.vSex = rblvSex.SelectedValue.ToString();
        Nova_MOD_Users.iUserStatus = rbliUserStatus.SelectedValue.ToString();
        Nova_MOD_Users.dDate = System.DateTime.Now;
        Nova_MOD_Users.WID = ddl_WareHs.SelectedValue;
    }

    private void ClearControl()
    {
        txtvUserNo.Text = "";
        txtvUserName.Text = "";
        txtvPassword.Text = "123456";
        //ddlvDepartment.SelectedIndex = 0;
        //ddlvGroup.SelectedIndex = 0;
        BindDepart();
        BindGroup();
        rbliUserStatus.SelectedValue = "1";
        rblvSex.SelectedValue = "0";
        txtvIpAddress.Text = "";

    }

}
